<template>
  <div class="login-container">
    <!-- 左侧插画 -->
    <div class="login-left">
      <img src="/img/login/leftImg.png" alt="插画">
    </div>
    <!-- 右侧输入框 -->
    <div class="login-right">
      <el-form
        ref="loginForm"
        :model="loginForm"
        class="login-form"
        autocomplete="on"
        label-position="left"
        :rules="rules"
      >
        <div class="title-container">
          <h3 class="title">BTG SaaS 商户平台</h3>
        </div>
        <el-form-item prop="account">
          <el-input v-model="loginForm.account" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" placeholder="请输入密码" :type="passwordType" />
          <span class="show-pwd" @click="clickEyes">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <p class="tips">忘记密码？</p>
        <el-button type="primary" style="width:100%;margin-bottom:30px;" :disabled="disabledStatus" @click="handleLogin">登入</el-button>
      </el-form>
    </div>
    <!-- 底部说明 -->
    <div class="login-bottom">
      <p>Copyright © 2020 武汉六个六科技有限公司 | 鄂ICP备20001932号-1</p>
    </div>
  </div>
</template>

<script>
import { getInit } from '@/api/user'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        account: 'ma52079134',
        // 15337208582
        password: '123456'
        // 698295
      },
      // 登录按钮禁用状态
      disabledStatus: true,
      // 判断密码类型
      passwordType: 'password',
      // 索引用来判断密码框得到焦点是否执行
      passwordIndex: 0,
      redirect: undefined,
      rules: {
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          {
            min: 10,
            max: 10,
            message: '所有登入账号都为十位有效字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码最小为六位有效字符', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    'loginForm.account': function() {
      this.butDisabledStats()
    },
    'loginForm.password': function() {
      this.butDisabledStats()
    }
  },
  created() {},
  mounted() {
    this.butDisabledStats()
  },
  destroyed() {},
  methods: {
    // 判断登入按钮是否为禁用状态
    butDisabledStats() {
      if (
        this.loginForm.account.length !== 10 ||
        this.loginForm.password.length < 6
      ) {
        this.disabledStatus = true
      } else {
        this.disabledStatus = false
      }
    },
    // 点击密码文本框中的眼睛
    clickEyes() {
      if (this.passwordType === 'password') {
        this.passwordType = 'text'
      } else {
        this.passwordType = 'password'
      }
    },
    handleLogin() {
      const Base64 = require('js-base64')
      // 深拷贝
      var newloginForm = JSON.parse(JSON.stringify(this.loginForm))
      newloginForm.password = Base64.encode(newloginForm.password)
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$store
            .dispatch('user/login', newloginForm)
            .then(() => {
              getInit().then((res) => {
                localStorage.setItem('init', JSON.stringify(res.data.data))
              }).catch().then(() => {
                this.$router.push('/home')
                localStorage.setItem(
                  'store',
                  JSON.stringify(this.$store.state)
                )
                sessionStorage.setItem('reload', '0')
              })
            })
            .catch(() => {
              this.$message({
                showClose: true,
                message: '账号或密码错误，登入失败',
                duration: 3000,
                type: 'error'
              })
            })
        } else {
          this.$message({
            showClose: true,
            message: '填写信息有误',
            duration: 3000,
            type: 'error'
          })
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#fff;
$light_gray:black;
$cursor: black;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-right .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-right {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 10px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border-bottom: 1px solid #C4C4C4;
    background: transparent;
    color: #454545;
  }
}
</style>
<style lang="scss" scoped>
$dark_gray:#889aa4;
$light_gray:#494949;

.login-right {
  min-height: 100%;
  width: 100%;
  overflow: hidden;
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 0 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }
  .tips{
    color: #A4A4A4;
    margin-top: -12px;
    text-align: right;
    margin-bottom: 36px;
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
<style lang="scss" scope>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  position: relative;
  height: 100%;
  width: 100%;
  background-image: url("/img/login/backgroundImg.png");
  background-size: 100% 100%;
  display: flex;
  .login-left {
    width: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    img {
      width: 100%;
      display: inline-block;
    }
  }
  .login-right {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 50%;
    .el-button {
      font-size: large;
      font-weight: bold;
    }
  }
  .login-bottom {
    position: absolute;
    bottom: 0px;
    right: 16px;
    color: black;
  }
}
</style>
